package tools.kmer;

import java.util.ArrayList;

import tools.sequences.sequenceUtils;

public class kmerFunctions {
	
	public static ArrayList<String> kmerToUse(StringBuffer seq, int kmerSize){
		ArrayList<String> kmers= new ArrayList<String>();
		if(seq.length()>=kmerSize){
			StringBuffer kmer=new StringBuffer(seq.substring(0, kmerSize));
			kmers.add(kmerToUse(kmer));
			for(int i=kmerSize;i<seq.length();++i){
				kmer.deleteCharAt(0);
				kmer.append(seq.charAt(i));
				kmers.add(kmerToUse(kmer));
			}
		}
		return kmers;
	}
	
	public static ArrayList<String> kmerToUse(String seq, int kmerSize){
		return kmerToUse(new StringBuffer(seq), kmerSize);
	}

	public static String kmerToUse(String kmer){
		return kmerToUse(new StringBuffer(kmer));
	}
	
	public static String kmerToUse(StringBuffer kmer){
		StringBuffer kmerRevComp= new StringBuffer();
		int j=kmer.length()-1,result;
		char c,r;
		for(int i=0;i<kmer.length();i++,j--){
			c=kmer.charAt(i);
			r=sequenceUtils.complement(kmer.charAt(j));
			if((result=c-r)!=0){
				if(result<0){
					return kmer.toString();
				}else{
					kmerRevComp.append(r);
					j--;
					break;
				}
			}else{
				kmerRevComp.append(r);
			}
		}
		for(;j>=0;j--){
			kmerRevComp.append(sequenceUtils.complement(kmer.charAt(j)));
		}
		return kmerRevComp.toString();
	}
	
	
}
